package ccnt.pvc.scudwaremobile.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpRequestHandlerRegistry;
import org.apache.http.protocol.HttpService;
import org.apache.http.protocol.ResponseConnControl;
import org.apache.http.protocol.ResponseContent;
import org.apache.http.protocol.ResponseDate;
import org.apache.http.protocol.ResponseServer;

import android.util.Log;
import ccnt.pvc.scudwaremobile.service.MainService;

/**
 * ScudwareMobile的server，对外开放接口
 * @author dy
 *
 */
public class ScudwareServer extends Thread {

	private static final String TAG = ScudwareServer.class.getName();
	private static final int PORT = 8080;
	private static final String WEBROOT = "/";
	private boolean isLoop = true;
	
	//MainService的实例
	private MainService mainService;
	
	public ScudwareServer(MainService mainService) {
		this.mainService = mainService;
	}
	
	@Override
	public void run() {
		Log.d(TAG, "server running");
		ServerSocket serverSocket = null;
		try {
			//创建服务器套接字
			serverSocket = new ServerSocket(PORT);
			//创建HTTP协议处理器
			BasicHttpProcessor httpProcessor = new BasicHttpProcessor();
			//增加HTTP协议拦截器
			httpProcessor.addInterceptor(new ResponseDate());
			httpProcessor.addInterceptor(new ResponseServer());
			httpProcessor.addInterceptor(new ResponseContent());
			httpProcessor.addInterceptor(new ResponseConnControl());
			//创建HTTP服务
			HttpService httpService = new HttpService(httpProcessor,
					new DefaultConnectionReuseStrategy(),
					new DefaultHttpResponseFactory());
			//创建HTTP参数
			HttpParams params = new BasicHttpParams();
			params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
					.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
							8 * 1024)
					.setBooleanParameter(
							CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
					.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
					.setParameter(CoreProtocolPNames.ORIGIN_SERVER,
							"WebServer/1.1");
			httpService.setParams(params);
			//创建HTTP请求执行器
			HttpRequestHandlerRegistry register = new HttpRequestHandlerRegistry();
			register.register("*", new HttpRootHandler(mainService));
			httpService.setHandlerResolver(register);
			
			//循环接收客户端连接
			while(isLoop && !Thread.interrupted()) {
				Log.d(TAG, "waiting for connection");
				Socket socket = serverSocket.accept();
				DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
				conn.bind(socket, params);
				
				Thread t = new HttpWorkerThread(httpService, conn);
				t.setDaemon(true);
				t.start();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(serverSocket != null) {
					serverSocket.close();
				}
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void close() {
		isLoop = false;
	}
}
